package jh.com.jhappbf.utils;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;

import org.xmlpull.v1.XmlPullParser;
import android.util.Xml;


/**
 * @author Administrator
 */
public class WebServiceConnection {
    public static String GetResultByService(String WebUrl, String nameSpace, String method, ArrayList<String> paramName,
                                            ArrayList<String> args) {
        String result = "";
        strError = "";
        String soap = ReadSoap(nameSpace, method, paramName, args);
        byte[] entity = soap.getBytes();
        try {
            HttpURLConnection conn = (HttpURLConnection) new URL(WebUrl).openConnection();
            conn.setConnectTimeout(5000);
            conn.setRequestMethod("POST");
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setRequestProperty("Content-Type", "application/soap+xml;charset=utf-8");
            conn.setRequestProperty("Content-Length", String.valueOf(entity.length));
            conn.getOutputStream().write(entity);
            if (conn.getResponseCode() == 200) {
                result = ParseSoap(conn.getInputStream());
            }
        } catch (Exception e) {
            // TODO: handle exception
            strError = "###" + e.toString();
        }
        return result;
    }



    public static String strError = "";

    private static String ParseSoap(InputStream inputStream) throws Exception {
        String result = null;
        XmlPullParser pullparser = Xml.newPullParser();
        pullparser.setInput(inputStream, "utf-8");
        int event = pullparser.getEventType();
        while (event != XmlPullParser.END_DOCUMENT) {
            switch (event) {
                case XmlPullParser.START_TAG:
                    if (pullparser.getName().contains("Result")) {
                        String s= pullparser.nextText();
                        return s;
                    }
                    break;
            }
            event = pullparser.next();
        }
        inputStream.close();
        return result;
    }

    private static String ReadSoap(String nameSpace, String method, ArrayList<String> paramName,ArrayList<String> args) {
        String soap = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
                + "<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" "
                + "xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">" + "<soap12:Body>";
        soap = soap + "<" + method + " xmlns=\"" + (nameSpace == null ? "http://tempuri.org" : nameSpace) + "/\">";

        String parm = "";
        for (int i = 0; i < paramName.size(); i++) {
            parm += "<{0}>{1}</{0}>".replace("{0}", paramName.get(i)).replace("{1}", args.get(i));
        }
        soap = soap + parm;
        soap = soap + "</" + method + ">";
        soap = soap + "  </soap12:Body></soap12:Envelope>";
        return soap;
    }


}
